package com.bobo.web.api.vo.base;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.bobo.web.api.annotation.IgnoreFieldToString;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.io.Serializable;
import java.lang.reflect.Field;

@JsonIgnoreProperties(
        ignoreUnknown = true
)
public class AbstractModel implements Serializable {

    @Override
    public String toString() {
        ReflectionToStringBuilder builder = new ReflectionToStringBuilder(
                this, ToStringStyle.SHORT_PREFIX_STYLE) {
            @Override
            protected boolean accept(Field field) {
                IgnoreFieldToString annotation = field.getAnnotation(IgnoreFieldToString.class);
                if (annotation != null) {
                    return false;
                }
                return true;
            }
        };
        return builder.toString();
    }

    @Override
    public boolean equals(Object o) {
        return EqualsBuilder.reflectionEquals(this, o);
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

}

